<?php
/**
 * Created by PhpStorm.
 * User: HJJ
 * Date: 2024-05-03
 * Time: 18:14:18
 * Info:
 */

namespace app\admin\controller\module;

use app\admin\annotation\ControllerAnnotation;
use app\admin\annotation\NodeAnotation;
use app\common\controller\AdminController;
use app\common\model\question as questionModel;
use think\Exception;
use think\App;
use lib\Random;
/**
 * @ControllerAnnotation(title="常见问题")
 * Class Node
 * @package app\admin\controller\module
 */
class QuestionController extends AdminController
{

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new questionModel();
    }

    /**
     * @NodeAnotation(title="常见问题列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $page  = (int)$this->request->param('page', 1);
            $limit = (int)$this->request->param('limit', 10);
            $first = ($page - 1) * $limit;
            $key   = $this->request->param('key');
            $where = function ($query) use ($key) {
                if ( ! empty($key['title'])) {
                    $query->whereLike('title', '%'.$key['title'].'%');
                }
                if (isset($key['status']) && $key['status'] != '') {
                    $query->where('status', $key['status']);
                }
            };
            $count = $this->model->where($where)->count();
            $list  = $this->model->field('id, title, status, create_time')->alias('a')->where($where)->limit($first, $limit)->order('sorts asc,id desc')->select();
            $data = [
                'code'  => 0,
                'msg'   => 'ok',
                'count' => $count,
                'data'  => $list
            ];

            return json($data);
        }

        return $this->fetch();
    }

    
    /**
     * @NodeAnotation(title="添加常见问题")
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $param = $this->request->param();
            $rule  = [
                'title|标题'   => 'require',
            ];
            $this->validate($param, $rule);

            $param['admin_id'] = cmf_get_admin_id();
            if (!empty($data['content'])) {
                $data['content'] = htmlspecialchars_decode($param['content']);
            }
            try {
                $param['create_time'] = time();
                $param['update_time'] = time();
                $id = $this->model->strict(false)->insertGetId($param);
                $this->success('保存成功');
            } catch (Exception $e) {
                $this->error('保存失败'.$e->getMessage());
            }
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="修改常见问题")
     */
    public function edit()
    {
        $id = $this->request->param('id');
        if (empty($id)) {
            $this->error('参数错误');
        }
        $data = $this->model->withTrashed()->find($id);
        if (empty($data)) {
            $this->error('获取数据失败');
        }
        if (!empty($data['content'])) {
            $data['content'] = htmlspecialchars_decode($data['content']);
        }
       
        if ($this->request->isAjax()) {
            $param = $this->request->param();
            $rule  = [
                'title|标题'   => 'require',
            ];
            $this->validate($param, $rule);
            try {
                $param['update_time'] = time();
                $res = $this->model->strict(false)->data($param)->update();
                if ($res) {
                    //写入tag标签
                    $this->success('保存成功');
                } else {
                    $this->error('保存失败');
                }
            } catch (Exception $e) {
                $this->error('保存失败'.$e->getMessage());
            }
        }
        $this->assign('data', $data);

        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="删除常见问题")
     */
    public function delete()
    {
        if ($this->request->isPost()) {
            $id = $this->request->param('id');
            if (empty($id)) {
                $this->error('参数错误');
            }
            $data = $this->model->onlyTrashed()->find($id);
            if (empty($data)) {
                $this->error('获取数据失败');
            }
            try {
                $this->model->destroy($id, true);
                $this->success('删除成功');
            } catch (Exception $e) {
                $this->error('删除失败'.$e->getMessage());
            }
        }
    }

}